package com.itmuch.cloud.comtroller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.itmuch.cloud.user.User;

@RestController
public class MovieController {

	@Autowired
	private RestTemplate restTemplate;
	
	@Autowired
	private LoadBalancerClient loadBalancerClient;
	
	
	@GetMapping("/movie/{id}")
	public User findById(@PathVariable("id") Long id) {
		//旧的：http://localhost:7900/simple/
		//新的叫做vip(虚拟IP)http://microservice-provider-user/simple/
		return restTemplate.getForObject("http://microservice-provider-user/simple/"+id, User.class);
	}
	
	@GetMapping("/test")
	public String test() {
		ServiceInstance choose = loadBalancerClient.choose("microservice-provider-user");
		ServiceInstance choose2 = loadBalancerClient.choose("microservice-provider-user2");
		System.out.println("choose:"+choose.getHost()+"  "+choose.getPort());
		System.out.println("choose2:"+choose2.getHost()+"  "+choose2.getPort());
		return "1";
	}
	
	
	
	
}
